export function getDate(type) {
	const date = new Date();

	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	let day = date.getDate();

	if (type === 'start') {
		year = year - 60;
	} else if (type === 'end') {
		year = year + 2;
	}
	month = month > 9 ? month : '0' + month;;
	day = day > 9 ? day : '0' + day;

	return `${year}-${month}-${day}`;
}

export function getDateTime(dateStr, type) {
	if (dateStr === null)
		return '';
	const dateTime = new Date(dateStr.replace(/-/g, "/"));

	let year = dateTime.getFullYear();
	let month = dateTime.getMonth() + 1;
	let day = dateTime.getDate();
	let hour = dateTime.getHours();
	let minute = dateTime.getMinutes();
	let second = dateTime.getSeconds();

	month = month > 9 ? month : '0' + month;;
	day = day > 9 ? day : '0' + day;
	hour = hour > 9 ? hour : '0' + hour;
	minute = minute > 9 ? minute : '0' + minute;
	second = second > 9 ? second : '0' + second;

	let result = ''
	switch (type) {
		case 'year':
			result = `${year}`;
			break;
		case 'date':
			result = `${month}-${day}`;
			break;
		case 'time':
			result = `${hour}:${minute}`;
			break;
		case 'full':
			result = `${year}-${month}-${day} ${hour}:${minute}:${second}`;
			break;
		case undefined:
		default:
			result = `${year}-${month}-${day}`;
			break;
	}

	return result;
}

export function compareDate(a, b) {
	var arr = a.split("-"); //log [2016,04,06]
	var start = new Date(arr[0], (arr[1] - 1), arr[2]);
	var starts = start.getTime(); //输出时间戳进行对比
	var arrs = b.split("-");
	var end = new Date(arrs[0], (arrs[1] - 1), arrs[2]);
	var ends = end.getTime();
	if (starts >= ends)
		return true
	else
		return false
}

export function previewImages(images, currentImage) {
	uni.previewImage({
		urls: images,
		current: currentImage,
	})
}

export function checkTimeInThreeMonths(begintime, endtime) {
	//判断时间跨度是否大于3个月
	var arr1 = begintime.split('-');
	var arr2 = endtime.split('-');
	arr1[1] = parseInt(arr1[1]);
	arr1[2] = parseInt(arr1[2]);
	arr2[1] = parseInt(arr2[1]);
	arr2[2] = parseInt(arr2[2]);
	var flag = true;
	if (arr1[0] == arr2[0]) { //同年
		if (arr2[1] - arr1[1] > 3) { //月间隔超过3个月
			flag = false;
		} else if (arr2[1] - arr1[1] == 3) { //月相隔3个月，比较日
			if (arr2[2] > arr1[2]) { //结束日期的日大于开始日期的日
				flag = false;
			}
		}
	} else { //不同年
		if (arr2[0] - arr1[0] > 1) {
			flag = false;
		} else if (arr2[0] - arr1[0] == 1) {
			if (arr1[1] < 10) { //开始年的月份小于10时，不需要跨年
				flag = false;
			} else if (arr1[1] + 3 - arr2[1] < 12) { //月相隔大于3个月
				flag = false;
			} else if (arr1[1] + 3 - arr2[1] == 12) { //月相隔3个月，比较日
				if (arr2[2] > arr1[2]) { //结束日期的日大于开始日期的日
					flag = false;
				}
			}
		}
	}
	if (!flag) {
		// alert("时间跨度不得超过3个月！");
		return false;
	}
	return true;
}

export function debounce(fn, time = 80) {
	let timer = null;
	return function () {
		clearTimeout(timer);
		let args = arguments;
		let self = this;
		timer = setTimeout(function () {
			fn.apply(self, args);
		}, time);
	}
}

export function delayFunc(time = 500) {
	return new Promise(resolve => {
		setTimeout(function () {
			resolve();
		}, time)
	})
}